package com.service.config.utils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用返回接口
 *
 * @ClassName Result
 * @Description TODO
 * @Author mmc
 * @Date 2020-04-14 16:00
 * @Version 1.0
 **/
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private static final int DEFAULT_ERR_CODE = 500;

    private static final String CODE = "code";
    private static final String MSG = "msg";

    private Integer code = 200;
    private String msg = "操作成功";
    private String description;
    private T data;

    private HashMap<String, Object> extend;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @JsonIgnore
    public HashMap<String, Object> getExtend() {
        return extend;
    }

    public void setExtend(HashMap<String, Object> extend) {
        this.extend = extend;
    }

    public Result() {
        extend = new HashMap<>();
    }

    public static Result failure(String msg) {
        return failure(DEFAULT_ERR_CODE, msg);
    }

    public static Result failure(int code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static Result ok(String msg) {
        Result result = new Result();
        result.put("msg", msg);
        return result;
    }

    public static Result ok(Map<String, Object> map) {
        Result result = new Result();
        result.extend.putAll(map);
        return result;
    }

    public static Result ok() {
        return new Result();
    }

    public Result put(String key, Object value) {
        extend.put(key, value);
        return this;
    }
}
